package com.pega.eprescription.activity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.pega.eprescription.R;
import com.pega.eprescription.database.manager.DatabaseManager;

public class DailyRemindActivity extends Activity {
	private int dayOfWeek;
	private Calendar calendar;
	private int dayOfYear;
	private ArrayList<String> history;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_daily_remind);

		calendar = Calendar.getInstance();
		dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);

		history = DatabaseManager.getInstance(this).getHistoryOfWeek(
				new Date(calendar.get(Calendar.YEAR),
						calendar.get(Calendar.MONTH),
						calendar.get(Calendar.DAY_OF_MONTH)), dayOfWeek);

		GridView gridview = (GridView) findViewById(R.id.gridview);
		gridview.setAdapter(new ImageAdapter(this));
		gridview.setVerticalScrollBarEnabled(false);

	}

	public class ImageAdapter extends BaseAdapter {
		private Context mContext;

		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return 32;
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		// create a new ImageView for each item referenced by the Adapter
		@SuppressLint("ResourceAsColor")
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = null;
			TextView textView = null;
			if (convertView == null) { // if it's not recycled, initialize some
										// attributes
				imageView = new ImageView(mContext);
				textView = new TextView(mContext);
				if (position < 8) {
					textView.setLayoutParams(new GridView.LayoutParams(
							ViewGroup.LayoutParams.MATCH_PARENT, 40));
					textView.setTextColor(mContext.getResources().getColor(
							R.color.White));
					textView.setTextSize(20);
					textView.setTypeface(null, Typeface.BOLD);
					textView.setGravity(Gravity.CENTER_VERTICAL
							| Gravity.CENTER_HORIZONTAL);
				} else {
					imageView.setLayoutParams(new GridView.LayoutParams(
							ViewGroup.LayoutParams.MATCH_PARENT, 120));
					imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
				}

				// imageView.setPadding(8, 8, 8, 8);
			} else {
				if (position < 8) {
					textView = (TextView) convertView;

				} else
					imageView = (ImageView) convertView;
			}

			if (position == 0) {
				return textView;
			} else if (position < 8) {
				if (position % 8 == dayOfWeek)
					textView.setBackgroundResource(R.color.Icon);
				else
					textView.setBackgroundResource(R.color.Black);
				if (position % 8 == Calendar.SUNDAY)
					textView.setText("SUN");
				if (position % 8 == Calendar.MONDAY)
					textView.setText("MON");
				if (position % 8 == Calendar.TUESDAY)
					textView.setText("TUE");
				if (position % 8 == Calendar.WEDNESDAY)
					textView.setText("WEN");
				if (position % 8 == Calendar.THURSDAY)
					textView.setText("THU");
				if (position % 8 == Calendar.FRIDAY)
					textView.setText("FRI");
				if (position % 8 == Calendar.SATURDAY)
					textView.setText("SAT");
				return textView;
			} else if (position == 8) {
				imageView.setBackgroundResource(R.color.AntiqueWhite);
				imageView.setImageResource(R.drawable.morning);
			} else if (position == 16) {
				imageView.setBackgroundResource(R.color.AntiqueWhite);
				imageView.setImageResource(R.drawable.afternoon);
			} else if (position == 24) {
				imageView.setBackgroundResource(R.color.AntiqueWhite);
				imageView.setImageResource(R.drawable.evening);
			} else {
				imageView.setImageResource(R.color.Azure);

				if (position % 8 == history.size()) {
					String a = history.get((position % 8) - 1);
					String[] b = a.split("");
					if (a.length() == 3) {
						if ((position / 8) == 1 && b[0].equals("0")) {
							imageView.setImageResource(R.color.Red);
						}
						if ((position / 8) == 2 && b[1].equals("0")) {
							imageView.setImageResource(R.color.Red);
						}
						if ((position / 8) == 3 && b[2].equals("0")) {
							imageView.setImageResource(R.color.Red);
						}
						imageView.setImageResource(R.color.Green);
					}
					if (a.length() == 2) {
						if ((position / 8) == 1 && b[0].equals("0")) {
							imageView.setImageResource(R.color.Red);
						}
						if ((position / 8) == 2 && b[1].equals("0")) {
							imageView.setImageResource(R.color.Red);
						}
						imageView.setImageResource(R.color.Green);
					}
					if (a.length() == 1) {
						if ((position / 8) == 1 && b[0].equals("0")) {
							imageView.setImageResource(R.color.Red);
						}
						imageView.setImageResource(R.color.Green);
					}
				} else if (position % 8 < history.size()) {
					String a = history.get((position % 8) - 1);
					String[] b = a.split("");
					if (a.length() > 0) {
						if ((position / 8) == 1 && b[0].equals("0")) {
							imageView.setImageResource(R.color.Red);
						}
						if ((position / 8) == 2 && b[1].equals("0")) {
							imageView.setImageResource(R.color.Red);
						}
						if ((position / 8) == 3 && b[2].equals("0")) {
							imageView.setImageResource(R.color.Red);
						}
						imageView.setImageResource(R.color.Green);
					}
				}
				
				if (position==9){
					imageView.setImageResource(R.color.Red);
					
				}
				
				if(position==17){
					imageView.setImageResource(R.color.Green);
				}

			}

			return imageView;
		}

	}
}
